'''
对openid 进行加密和解密操作
'''
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
# 加密
def generic_openid(openid):

    # 1. 创建一个实例对象
    # secret_key     秘钥
    # expires_in     数据过期时间  单位：秒
    s = Serializer(secret_key='666',expires_in=3600)
    # 2.组织数据，加密数据
    data = {
        'openid':openid
    }

    secret_data = s.dumps(data)
    # 3.返回加密数据
    # bytes --> str
    return secret_data.decode()


# 解密
from itsdangerous import BadSignature
def check_token(token):

    # 1.创建一个实例对象
    s = Serializer(secret_key='666',expires_in=3600)
    # 2.解密数据(异常捕获)
    try:
        data = s.loads(token)
    except BadSignature:
        return
    # 3.退回解密的数据
    return data.get('openid')

